package com.wtcrazy.controller;

import com.wtcrazy.entity.*;
import com.wtcrazy.service.Impl.UserServiceImpl;
import com.wtcrazy.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController//必须的
@RequestMapping("/user")//标注于方法体上，用于指定访问路径
//控制器层,接收前端传过来的参数进行业务操作
//暴露API到网络上,只负责接收数据和返回数据,对数据进行的任何复杂的处理都需要调用Service层的方法。
public class UserController {
    @Autowired//将属性注入
    private UserServiceImpl userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")//用户登录
    public JsonResult login(@RequestBody User user) {//@RequestBody将前端json转后台Java
        return userService.login(user);
    }

    @ApiOperation("注销账号")
    @DeleteMapping("/deleteMyself")//
    public JsonResult deleteMyself(@RequestBody String uid) {
        return userService.deleteMyself(uid);
    }

    @ApiOperation("用户注册功能")
    @PostMapping("/register")//
    public JsonResult register(@RequestBody User user) {
        return userService.register(user);
    }

    @ApiOperation("找回密码")
    @PutMapping("/findPassword")
    public JsonResult findPassword(@RequestBody Object obj) {
        return userService.findPassword(obj);
    }

    @ApiOperation("注册邮箱功能")
    @GetMapping("/register/sendMailCode")//
    public String sendMailCode(@RequestParam String mail) {
        return userService.sendMailCode(mail);
    }

    @ApiOperation("检验token")
    @GetMapping("/checkToken") //
    public JsonResult checkToken(@RequestParam String Token) {
        return userService.checkToken(Token);
    }

    @ApiOperation("查询帖子")
    @GetMapping("/getAllPosts")//
    public List<Object> getAllPosts() {
        return userService.getAllPosts();
    }

    @ApiOperation("查询热门帖子")
    @GetMapping("/getAllHotPosts")//
    public List<Object> getAllHotPosts() {
        return userService.getAllHotPosts();
    }

    @ApiOperation("分页查询帖子")
    @GetMapping("/getLimitPosts")//
    public List<Object> getLimitPosts(@RequestParam Integer startIndex, Integer pageSize) {
        return userService.getLimitPosts(startIndex, pageSize);
    }

    @ApiOperation("分页查询热门帖子")
    @GetMapping("/getLimitHotPosts")//
    public List<Object> getLimitHotPosts(@RequestParam Integer startIndex, Integer pageSize) {
        return userService.getLimitHotPosts(startIndex, pageSize);
    }

    @ApiOperation("【拦截】获取帖子详情")
    @GetMapping("/getPost")//【GET请求不能用@RequestBody来接收参数】
    public Post getPost(@RequestParam String id) {
        return userService.getPost(id);
    }

    @ApiOperation("【拦截】新增帖子")
    @PostMapping("/uploadPost")
    public Post uploadPost(@RequestBody Post post) {
        return userService.uploadPost(post);
    }

    @ApiOperation("【拦截】删除帖子")
    @DeleteMapping("/deletePost")
    public Object deletePost(@RequestBody String postid) {
        return userService.deletePost(postid);
    }

    @ApiOperation("增加帖子浏览量")
    @PutMapping("/addPostView")
    public String addPostView(@RequestBody String id) {//用@Requestparam接收id为null
        return userService.addPostView(id);
    }

    @ApiOperation("查询是否点赞")
    @PostMapping("/isLikePost")
    public PostLike isLikePost(@RequestBody PostLike postLike) {
        return userService.isLikePost(postLike);
    }

    @ApiOperation("【拦截】点赞")
    @PostMapping("/likePost")
    public PostLike likePost(@RequestBody PostLike postLike) {
        return userService.likePost(postLike);
    }

    @ApiOperation("【拦截】取消点赞")
    @DeleteMapping("/unLikePost")
    public String unLikePost(@RequestBody PostLike postLike) {
        return userService.unLikePost(postLike);
    }

    @ApiOperation("【拦截】更新帖子表的点赞数量")
    @PutMapping("/updatePostLike")
    public String updatePostLike(@RequestBody String jsonStr) {
        return userService.updatePostLike(jsonStr);
    }

    @ApiOperation("【拦截】更新帖子表的评论数量")
    @PutMapping("/updatePostComment")
    public String updatePostComment(@RequestBody String jsonStr) {
        return userService.updatePostComment(jsonStr);
    }

    @ApiOperation("【拦截】查询我发布的帖子")
    @PostMapping("/getMyPosts")
    public List<String> getMyPosts(@RequestBody String uid) {
        return userService.getMyPosts(uid);
    }

    @ApiOperation("【拦截】分页查询我发布的帖子")
    @PostMapping("/getLimitMyPosts")
    public List<Object> getLimitMyPosts(@RequestBody Object obj) {
        return userService.getLimitMyPosts(obj);
    }

    @ApiOperation("【拦截】分页查询我发布的热门帖子")
    @PostMapping("/getLimitMyHotPosts")
    public List<Object> getLimitMyHotPosts(@RequestBody Object obj) {
        return userService.getLimitMyHotPosts(obj);
    }

    @ApiOperation("【拦截】用户更新信息")
    @PutMapping("/updateUserInfo")
    public Object updateUserInfo(@RequestBody Object obj) {
        return userService.updateUserInfo(obj);
    }

    @ApiOperation("【拦截】查询用户所有的点赞的列表帖子")
    @PostMapping("/getUserAllLikePosts")
    public List<PostLike> getUserAllLikePosts(@RequestBody Object obj) {
//        System.out.println("id===" + id);
        return userService.getUserAllLikePosts(obj);
    }

    @ApiOperation("【拦截】发表评论")
    @PostMapping("/submitComment")
    public Comment submitComment(@RequestBody Comment comment) {
        return userService.submitComment(comment);
    }

    @ApiOperation("【拦截】获取该帖子下所有的一级评论")
    @GetMapping("/getAllCommment")
    public List<Comment> getAllCommment(@RequestParam(required = false, name = "id") String id) {
        return userService.getAllCommment(id);
    }

    @ApiOperation("【拦截】获取该帖子下回复该评论的所有的二级评论")
    @GetMapping("/getAllSecondCommment")
    public List<Comment> getAllSecondCommment(@RequestParam String postId, @RequestParam String replyCommentId) {
        return userService.getAllSecondCommment(postId, replyCommentId);
    }

    @ApiOperation("【拦截】删除评论")
    @DeleteMapping("/deleteComment")
    public Object deleteComment(@RequestBody Object obj) {
        userService.deleteComment(obj);
        return obj;
    }

    @ApiOperation("【拦截】改变帖子表的评论量")
    @PutMapping("/changePostComment")
    public Object changePostComment(@RequestBody Object obj) {
        userService.changePostComment(obj);
        return obj;
    }

    @ApiOperation("【拦截】获取用户在该帖子下的评论列表")
    @PostMapping("/getUserComment")
    public List<Comment> getUserComment(@RequestBody Object obj) {
        return userService.getUserComment(obj);
    }

    @GetMapping("/getLimitArticle")
    public List<Article> getLimitArticle(@RequestParam Integer classid, @RequestParam Integer page, @RequestParam Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        return userService.getLimitArticle(classid, start, pageSize);
    }

    @ApiOperation("用户进入文章详情")
    @GetMapping("/getArticle")
    public Article getArticle(@RequestParam String id) {
        return userService.getArticle(id);
    }

    @ApiOperation("【拦截】查询回复该一级评论的二级评论总数")
    @GetMapping("/querySecondCommentNum")
    public Integer querySecondCommentNum(@RequestParam String commentId) {
        return userService.querySecondCommentNum(commentId);
    }

    @ApiOperation("【拦截】获得用户点赞、发帖、获评")
    @GetMapping("/getUserInfoNum")
    public List getUserInfoNum(@RequestParam String uid) {
        List<Integer> list = new ArrayList<>();
        list.add(userService.getUserInfoLike(uid));
        list.add(userService.getUserInfoPost(uid));
        list.add(userService.getUserInfoComment(uid));
        return list;
    }
}
